#include "qmediaplaylist.h"

QMediaPlaylist::QMediaPlaylist(QObject *parent)
    :QObject(parent)
    , currentIndex(-1)
    , mode(PlaybackMode::Loop)
{}

// 添加媒体文件
void QMediaPlaylist::addMedia(const QUrl &url) {
    if(currentIndex < 0){
        currentIndex = 0;
    }
    mediaList.append(url);
}

// 设置播放模式
void QMediaPlaylist::setPlaybackMode(PlaybackMode mode) {
    if (this->mode != mode) {
        this->mode = mode;
        emit playbackModeChanged(mode); // 发射播放模式改变信号
    }
}

// 获取下一个媒体文件
QUrl QMediaPlaylist::next() {
    if (mediaList.isEmpty()) return QUrl();

    int previousIndex = currentIndex;
    int index = currentIndex;

    switch (mode) {
    case PlaybackMode::Sequential:
        index = (currentIndex + 1) % mediaList.size();
        if (currentIndex == 0 && previousIndex != -1) {
            // 顺序播放模式下，播完后停止
            return QUrl();
        }
        break;
    case PlaybackMode::Loop:
        index = (currentIndex + 1) % mediaList.size();
        break;
    case PlaybackMode::Random:
        index = QRandomGenerator::global()->bounded(mediaList.size());
        break;
    case PlaybackMode::CurrentItemLoop:
        // 保持 currentIndex 不变
        index = currentIndex;
        break;
    }

    if (index != previousIndex) {
        setCurrentIndex(index);
    }

    return mediaList.at(currentIndex);
}

// 获取上一个媒体文件
QUrl QMediaPlaylist::previous() {
    if (mediaList.isEmpty()) return QUrl();

    int previousIndex = currentIndex;
    int index = currentIndex;

    switch (mode) {
    case PlaybackMode::Sequential:
    case PlaybackMode::Loop:
        index = (currentIndex - 1 + mediaList.size()) % mediaList.size();
        break;
    case PlaybackMode::Random:
        index = QRandomGenerator::global()->bounded(mediaList.size());
        break;
    case PlaybackMode::CurrentItemLoop:
        // 保持 currentIndex 不变
        index = currentIndex;
        break;
    }

    if (index != previousIndex) {
        setCurrentIndex(index);
    }

    return mediaList.at(currentIndex);
}

// 获取当前媒体文件
QUrl QMediaPlaylist::currentMedia() const {
    if (currentIndex >= 0 && currentIndex < mediaList.size()) {
        return mediaList.at(currentIndex);
    }
    return QUrl();
}

//设置当前播放索引
void QMediaPlaylist::setCurrentIndex(int index) {

    if (index >= mediaList.size()) {
        currentIndex = mediaList.size() - 1;
    } else if (index < 0) {
        currentIndex = 0;
    } else {
        currentIndex = index;
    }
    //qDebug() << currentIndex;

    emit currentIndexChanged(currentIndex); // 发射当前索引改变信号
    //qDebug() << "33333";
    emit currentMediaChanged(mediaList.at(currentIndex)); // 发射当前媒体改变信号
}

//获取当前索引
int QMediaPlaylist::getCurrentIndex()const
{
    //qDebug() << currentIndex;
    return currentIndex;
}

PlaybackMode QMediaPlaylist::playbackMode() const
{
    return mode;
}

void QMediaPlaylist::clear()
{
    mediaList.clear();
    currentIndex  = -1;
}

void QMediaPlaylist::sendIndexAndMediaChanged(int index, QUrl url)
{
    emit currentIndexChanged(index);
    emit currentMediaChanged(url);
}
